package pers.cl.dubbo.provider.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.AsyncContext;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.cl.dubbo.api.model.User;
import pers.cl.dubbo.api.service.CallBackListener;
import pers.cl.dubbo.api.service.UserService;

import java.util.concurrent.CompletableFuture;

/**
 * @author chenlei
 * @date 2020/1/10
 * @description
 **/
@Service(group = "user2")
public class UserServiceImpl2 implements UserService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String sayHello(User user) {
        logger.info("dubbo调用成功"+user.getId()+user.getUserName());
        return "hello,userServieImpl2";
    }

    @Override
    public CompletableFuture<String> async(User user) {
        final AsyncContext asyncContext = RpcContext.startAsync();
        new Thread(() -> {
            // 如果要使用上下文，则必须要放在第一句执行
            asyncContext.signalContextSwitch();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 写回响应
            asyncContext.write("Hello " + user.getId()+user.getUserName() + ", response from provider.");
        }).start();
        return null;
    }

    @Override
    public void addListener(String key, CallBackListener listener) {

    }
}
